package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Coupon;
import com.chixing.entity.Mycoupon;
import com.chixing.mapper.MycouponMapper;
import com.chixing.service.ICouponService;
import com.chixing.service.IMycouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */
@Service
public class MycouponServiceImpl implements IMycouponService {
    @Autowired
    private MycouponMapper mycouponMapper;
    @Autowired
    private ICouponService couponService;
    @Override
    public Result getAll() {
        //select *from mycoupon
        QueryWrapper<Mycoupon> wrapper = new QueryWrapper<>();

        List<Mycoupon> mycouponList = mycouponMapper.selectList(wrapper);
        if(mycouponList !=null && mycouponList.size()>0)
            return Result.getSuccess(mycouponList);
        return Result.getFail("暂无优惠卷数据");
    }

    @Override
    public Result getMycouponByCustomerId(Integer customerId,Integer status) {
        QueryWrapper<Mycoupon> wrapper = new QueryWrapper<>();//where
        wrapper.eq("cust_id",customerId).eq("status",status).orderByDesc("mycoupon_create_date");
        //
        //我领取的优惠卷信息
        List<Mycoupon> mycouponList = mycouponMapper.selectList(wrapper);
        for(Mycoupon mycoupon:mycouponList){
            //优惠卷信息
            Result couponResult =couponService.getCouponById(mycoupon.getCouponId());
            String couponName =((Coupon)couponResult.getData()).getCouponName();
            Float couponPrice =((Coupon)couponResult.getData()).getCouponPrice();
            mycoupon.setOther1(couponName);
            mycoupon.setOther2(couponPrice.toString());
        }
        if(mycouponList !=null && mycouponList.size()>0)
            return Result.getSuccess(mycouponList);
        return Result.getFail("您暂无有效优惠卷");
    }

    @Override
    public Result save(Mycoupon mycoupon) {
        // insert into mycoupon ....
        // rows 表示 dish表中受影响行数 , rows = 1 表示 添加了一条记录成功
        int rows = mycouponMapper.insert(mycoupon);
        if(rows >0)
            return Result.saveSuccess(mycoupon);
        return Result.saveFail(mycoupon);
    }
}
